// pages/confirmOrder/confirmOrder.js
const app = getApp();
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    transfer: null,
    address: "",
    baseDomain: app.globalData.baseDomain,
    apiDomain: app.globalData.apiDomain
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var transferId = options.transferId;
    wx.request({
      url: app.globalData.apiDomain + '/api/address/find',
      method: "POST",
      data: {
        openId: app.globalData.openId
      },
      success: function(res) {
        console.log(res.data.data[0].address);
        that.setData({
          address: res.data.data[0].address
        });
      }
    });
    wx.request({
      url: app.globalData.apiDomain + '/api/transfer/getTransferDetail/' + transferId,
      success(res) {
        var obj = JSON.parse(res.data.data.contentImg);
        that.setData({
          transfer: res.data.data
        });
      }
    })
    // wx.request({
    //   url: app.globalData.apiDomain + '/api/transfer/getTransferDetail/' + transferId,
    //   success(res) {
    //     console.log(res.data);
    //     that.setData({transfer: res.data.data });
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  confirmOrder: util.throttle(function(e) {
    var transferId = e.currentTarget.dataset.transferId;
    var payAmount = e.currentTarget.dataset.payAmount;
    wx.request({
      url: app.globalData.apiDomain + '/api/order/confirmOrder',
      method: "POST",
      data: {
        transferId: transferId,
        openId: app.globalData.openId,
        payAmount: payAmount
      },
      success:function(e){
        wx.redirectTo({
          url: '../order/order',
          complete: function () {
            wx.showToast({
              title: '下单成功',
              image: '../../images/correct_toast.png',
              duration: 2000,
            });
          }
        })
      }
    })
  })
})